<template>
	<div class="addchangecourses">
		<a class="navc">申请换课</a>
		<span class="backchangecourses" @click="backchangecourses">返回</span>
		<el-form ref="myForm" class="addform" :model="formData" :rules="rules">
			<el-form-item label="申 请 老 师 :" prop="teacherName">
				<el-input class="inputs" v-model="teacherName" placeholder="请输入申请老师名称" disabled></el-input>
			</el-form-item>
			<el-form-item label="指 向 老 师 :" prop="toteacherName">
				<el-select placeholder="请选择指向老师" v-model="formData.toteacherName"
					@change="gettoteacherid(formData.toteacherName)">
					<el-option v-for="item in teacherlist" :key="item.value" :label="item.teaName" :value="item.teaId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="申 请 课 程 :" prop="changecourse">
				<el-select placeholder="请选择申请课程" v-model="formData.changecourse"
					@change="getchangecourseid(formData.changecourse)">
					<el-option v-for="item in courses" :key="item.value" :label="item.subName" :value="item.subId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="指 向 课 程 :" prop="tochangecourse">
				<el-select placeholder="请选择指向课程" v-model="formData.tochangecourse"
					@change="gettochangecourseid(formData.tochangecourse)">
					<el-option v-for="item in tocourses" :key="item.value" :label="item.subName" :value="item.subId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="申 请 班 级 :" prop="changeclass">
				<el-select placeholder="请选择申请班级" v-model="formData.changeclass"
					@change="getchangeclassid(formData.changeclass)">
					<el-option v-for="item in classlist" :key="item.value" :label="item.clazzName" :value="item.clazzId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="指 向 班 级 :" prop="tochangeclass">
				<el-select placeholder="请选择指向班级" v-model="formData.tochangeclass"
					@change="gettochangeclassid(formData.tochangeclass)">
					<el-option v-for="item in toclasslist" :key="item.value" :label="item.clazzName" :value="item.clazzId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="原上课时间 :" prop="beforedate">
				<!-- <el-input class="inputs" v-model="formData.beforedate" placeholder="请输入周几第几节"></el-input> -->
				<el-select placeholder="请选择原上课时间" v-model="formData.beforedate">
					<el-option v-for="item in onclasslist" :key="item.value" :label="item.time" :value="item.time">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="换 课 时 间 :" prop="newdate">
				<!-- <el-input class="inputs" v-model="formData.newdate" placeholder="请输入周几第几节"></el-input> -->
				<el-select placeholder="请选择原上课时间" v-model="formData.newdate">
					<el-option v-for="item in toonclasslist" :key="item.value" :label="item.time" :value="item.time">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="申 请 日 期 :" prop="repuestdate">
				<el-input class="inputs" v-model="nowTime" placeholder="请输入申请日期" disabled></el-input>
			</el-form-item>
			<el-form-item label=" 审 批 人 :" prop="approver" class="last">
				<el-input class="inputs" v-model="formData.approver" placeholder="请输入审批人" disabled></el-input>
			</el-form-item>
		</el-form>
		<el-row>
			<el-button type="primary" @click="addchangecourses('myForm')" class="btn addchangebtn">确定</el-button>
			<el-button class="btn" type="info" @click="clearinput('myForm')">取消</el-button>
		</el-row>
	</div>
</template>

<script>
export default {
	data() {
		return {
			nowTime: "",
			teacherName: "",
			formData: {
				teacherId: "",//申请人id
				teacherName: "",
				toteacherName: "",
				changecourse: "",
				tochangecourse: "",
				changeclass: "",
				tochangeclass: "",
				repuestdate: "",
				newdate: "",
				approver: "",//审批人
				beforedate: "",
				userId: "",
			},
			rules: {
				// teacherName: [{ required: true, message: "申请老师不能为空", trigger: "blur" }],
				toteacherName: [{ required: true, message: "指向老师不能为空", trigger: "change" }],
				// repuestdate: [{ required: true, message: "申请日期不能为空", trigger: "blur" }],
				changecourse: [{ required: true, message: "申请课程不能为空", trigger: "change" }],
				tochangecourse: [{ required: true, message: "指向课程不能为空", trigger: "change" }],
				changeclass: [{ required: true, message: "申请班级不能为空", trigger: "change" }],
				tochangeclass: [{ required: true, message: "指向班级不能为空", trigger: "change" }],
				newdate: [{ required: true, message: "调课时间不能为空", trigger: "blur" }],
				approver: [{ required: true, message: "审批人不能为空", trigger: "change" }],
				beforedate: [{ required: true, message: "原上课时间不能为空", trigger: "blur" }],
			},
			username: "",
			courses: [],
			tocourses: [],
			classlist: [],
			toclasslist: [],
			teacherlist: [],
			baseUrl: require("../js/baseUrl.js").baseUrl,
			currentpage: 1, //当前第几页
			pageSize: 5, //每页显示五条,
			total: 0,
			total1: 0,
			approverlist: [], //审批人列表
			toteacherid: "", //选中指向老师id
			changecourseid: "", //选中申请课程id
			tochangecourseid: "", //选中指向课程id
			changeclassid: "", //选中申请班级id
			tochangeclassid: "", //选中指向班级id
			approverid: "", //审批人id,
			tea: [],//班级负责人信息
			onclass: [],//申请老师正在上所有课
			onclasslist: [],//申请老师正在上的该班的课
			toonclass: [],//指向老师正在上所有课
			toonclasslist: [],//指向老师正在上的该班的课
		}
	},
	methods: {
		backchangecourses() { //返回上一级
			this.$router.push("/coursemanager/changecourse/changecourses")
		},
		//申请换课
		requestchange() {
			this.$http.post(this.baseUrl + "SchoolSys/cha/application", {
				userId: Number(this.formData.userId)
			}).then(res => {
				//console.log("申请换课", res);
				if (res.data.code == 200) {
					this.teacherName = res.data.data.list[0].teaName
					this.formData.teacherId = res.data.data.list[0].teaId//申请老师id
					this.courses = res.data.data.list[0].subs//申请老师的课程
					this.teacherlist = res.data.data.pointTeacher//所有指向老师
				} else {
					this.$message.error(res.data.msg)
				}
			})
		},
		//添加换课
		addchangecourses(form) {
			this.$refs[form].validate(valid => {
				if (valid) {
					this.$http.post(this.baseUrl + "SchoolSys/cha/addCha", {
						oldTeaId: Number(this.formData.teacherId),
						oldTime: this.formData.beforedate,
						oldClazzId: this.changeclassid,
						oldSubId: this.changecourseid,
						newTeaId: this.toteacherid,
						newTime: this.formData.newdate,
						newClazzId: this.tochangeclassid,
						newSubId: this.tochangecourseid,
						userId: this.approverid,
						status: 0,
						createTime: this.nowTime
					}).then(res => {
						//console.log(res);
						if (res.data.code == 200) {
							this.$message({
								message: '申请成功',
								type: 'success'
							});
							this.$router.go(-1);
						}
					})
				} else {
					this.$message({
						message: '您存在未填写信息！！！',
						type: 'warning'
					});
				}
			})
		},
		//获取选中申请课程id，根据课程查班级
		getchangecourseid(changecourseid) {
			this.classlist = []
			this.onclass = []
			// console.log(changecourseid);
			this.changecourseid = changecourseid;
			this.$http.post(this.baseUrl + "SchoolSys/rea/applicationBySubId", {
				subId: this.changecourseid,
				teaId: this.formData.teacherId
			}).then(res => {
				if (res.data.code == 200) {
					let classarr = res.data.data
					classarr.forEach(v => {
						//console.log(v);
						this.classlist.push(v.clazzes[0])//申请老师的课程的班级
						this.onclass.push(v.clazzes[0])//正在上的所有课
					});
					//console.log("申请老师的课程的班级", this.classlist);
				} else {
					this.$message.error(res.data.msg)
				}
			})
		},
		//获取选中申请班级id
		getchangeclassid(changeclass) {
			this.onclasslist = []
			//console.log("班级信息", changeclass);
			this.changeclassid = changeclass
			this.classlist.map(v => {
				if (changeclass == v.clazzId) {
					this.tea = v.tea
				}
			})
			this.approverid = this.tea.teaId
			this.formData.approver = this.tea.teaName
			this.onclass.map(v => {
				if (changeclass == v.clazzId) {
					this.onclasslist.push(v)
				}
			})
			//console.log("正在上的该班的课", this.onclasslist);
			this.getclasstime();
		},
		//获取选中指向老师id
		gettoteacherid(toteacherid) {
			//console.log(toteacherid);
			this.toteacherid = toteacherid;
			//SchoolSys/cha/pointSub
			this.$http.post(this.baseUrl + "SchoolSys/cha/pointSub", {
				teaId: this.toteacherid
			}).then(res => {
				//console.log("指向老师信息",res);		
				if (res.data.code == 200) {
					this.tocourses = res.data.data.subs//指向老师的课程
				} else {
					this.$message.error(res.data.msg)
				}
			})
		},
		//获取选中指向课程id，根据课程查指向班级
		gettochangecourseid(tochangecourseid) {
			this.toclasslist = []
			this.toonclass=[]
			//console.log(tochangecourseid);
			this.tochangecourseid = tochangecourseid;
			//SchoolSys/cha/pointClazz
			this.$http.post(this.baseUrl + "SchoolSys/cha/pointClazz", {
				subId: this.tochangecourseid,
				teaId: this.toteacherid
			}).then(res => {
				//console.log("指向课程对应班级", res);
				if (res.data.code == 200) {
					let classarr = res.data.data
					classarr.forEach(v => {
						this.toclasslist.push(v.clazzes[0])//申请老师的课程的班级
					});
				} else {
					this.$message.error("暂无数据")
				}
			})
		},
		//获取选中指向班级信息
		gettochangeclassid(tochangeclassid) {
			this.toonclasslist=[]
			//console.log(tochangeclassid);
			this.tochangeclassid = tochangeclassid;//指向班级id
			this.toonclass.map(v => {
				if (tochangeclassid == v.clazzId) {
					this.toonclasslist.push(v)
				}
			})
			//console.log("指向老师正在上的该班的课", this.toonclasslist);
			this.gettoclasstime();
			
		},
		//根据老师课程班级查该老师在该班上该课程是周几第几节
        getclasstime(){
            this.$http.post(this.baseUrl + "SchoolSys/cha/getSchaById", {
                teaId:this.formData.teacherId,
                subId:this.changecourseid,
                clazzId:this.changeclassid,
            }).then(res => {
                //console.log("该老师在该班上该课程是周几第几节", res);
                if (res.data.code == 200) {
                    this.onclasslist=res.data.data
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
		//根据指向老师课程班级查该老师在该班上该课程是周几第几节
        gettoclasstime(){
            this.$http.post(this.baseUrl + "SchoolSys/cha/getSchaById", {
                teaId:this.toteacherid,
                subId:this.tochangecourseid,
                clazzId:this.tochangeclassid,
            }).then(res => {
                //console.log("该指向老师在该班上该课程是周几第几节", res);
                if (res.data.code == 200) {
                    this.toonclasslist=res.data.data
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
		//清空
		clearinput(formData) {
			this.$refs[formData].resetFields();
		},
	},
	mounted() {
		//获取申请人Id
		let date = new Date();
		let dates = date.toLocaleDateString()
		this.nowTime = dates.replaceAll("/", "-")
		this.formData.userId = sessionStorage.getItem("userId");
		this.requestchange();
	}
}
</script>

<style lang="less">
.addchangecourses {
	position: relative;
	margin-top: 20px;
	height: 40px;
	line-height: 40px;

	.navc {
		text-decoration: none;
		margin-left: 30px;
		color: #000;
		font-size: 20px;
		font-weight: 900;
	}

	.backchangecourses {
		position: absolute;
		right: 50px;
		top: 0;
		font-size: 20px;
	}

	.el-row {
		width: 95%;
		margin: 200px auto 0px;

		.btn {
			width: 140px;
			height: 50px;
			font-size: 15px;
		}

		.addchangebtn {
			background: #106ab1;
		}
	}

	.addform {
		width: 96%;
		height: 320px;
		margin: 30px auto 0px;

		.el-form-item {
			width: 34%;
			float: left;

			.el-form-item__label {
				font-size: 16px;
				font-weight: 600;
			}

			.el-select {
				width: 100%;
			}

			.el-date-editor {
				width: 100%;
			}
		}

		.el-form-item__content {
			width: 65%;
			float: left;
		}

		.last {
			.el-form-item__content {
				margin-left: 18px;
			}
		}

		.el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap>.el-form-item__label:before,
		.el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
			content: "";
		}
	}
}
</style>